import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import './home/home_page.dart';
import './category/category_page.dart';
import './shopcart/shopcart_page.dart';
import './member/member_page.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';
import '../provider/route_change.dart';

class IndexPage extends StatefulWidget {
  IndexPage({Key key}) : super(key: key);

  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  final List<BottomNavigationBarItem> bottomTabs = [
    BottomNavigationBarItem(title: Text('首页'), icon: Icon(CupertinoIcons.home)),
    BottomNavigationBarItem(
        title: Text('分类'), icon: Icon(CupertinoIcons.search)),
    BottomNavigationBarItem(
        title: Text('购物车'), icon: Icon(CupertinoIcons.shopping_cart)),
    BottomNavigationBarItem(
        title: Text('会员中心'), icon: Icon(CupertinoIcons.profile_circled))
  ];

  final bodies = [HomePage(), CategoryPage(), ShopCartPage(), MemberPage()];

  // int _currentIndex = 0;
  // var currentPage;

  @override
  void initState() {
    super.initState();

    // _currentIndex = 0;
    // currentPage = bodies[_currentIndex];
  }

  @override
  Widget build(BuildContext context) {
    // 屏幕适配
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    return Provide<RouteChangeProvide>(
        builder: (context, child, routeChangeProvide) {
      return Scaffold(
        backgroundColor: Color.fromRGBO(244, 245, 245, 1.0),
        bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          items: bottomTabs,
          // currentIndex: _currentIndex,
          currentIndex: routeChangeProvide.currentIndex,
          onTap: (index) {
            setState(() {
              // _currentIndex = index;
              // currentPage = bodies[_currentIndex];
              Provide.value<RouteChangeProvide>(context).changeRoute(index);
            });
          },
        ),
        // body: currentPage,
        body: IndexedStack(
          // index: _currentIndex,
          index: routeChangeProvide.currentIndex,
          children: bodies,
        ),
      );
    });
  }
}
